<?php
defined('_JEXEC') or die('Restricted access');

jimport('joomla.application.component.model');
class DocarioModelCategory extends JModel {
    const CATEGORY_ALLOW_READ = 0;
    const CATEGORY_ALLOW_WRITE = 1;
    
    const CATEGORY_ALLOWED = 0;
    const CATEGORY_NOT_ALLOWED = 1;
    const CATEGORY_INHERITED = 2;
    
    const CATEGORY_GROUP_NOT_ALLOWED = 0;
    const CATEGORY_GROUP_ALLOWED = 1;
    
    const CATEGORY_PUBLISHED = 1;
    const CATEGORY_UNPUBLISHED = 0;

    var $_data;
    var $_total;
    var $_id;
    
    var $_category;
    
    var $_query;
    
    var $_pagination;
    
    function __construct() {
        parent::__construct();
        
        $mainframe = JFactory::getApplication();
        
        $limit = $mainframe->getUserStateFromRequest('global.list.limit','limit',$mainframe->getCfg('list_limit'), 'int');
		$limitstart = JRequest::getVar('limitstart', 0, '', 'int');
		$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
		$this->setState('limit', $limit);
		$this->setState('limitstart', $limitstart);   
    }
    
    function setId($id) {
        $this->_id = $id;
    }
    
    function getPagination() {
        if (empty($this->_pagination)) {
            jimport('joomla.html.pagination');
            $this->_pagination = new JPagination($this->getTotal(),
                                                 $this->getState('limitstart'),
                                                 $this->getState('limit'));
        }
        return $this->_pagination;
    }
    
    function getTotal() {
        $this->_buildQuery();
        if (empty($this->_total)) {
            $this->_total = $this->_getListCount($this->_query);
        }
        return $this->_total;
    }
    
    function _buildQuery() {
        $this->_query = 'select * from #__docario_category';
        return $this->_query;
    }
    
    function getData() {
        $this->_buildQuery();
        if (empty($this->_data))
            $this->_data = $this->_getList($this->_query, 
                                           $this->getState('limitstart'),
                                           $this->getState('limit'));
        return $this->_data;
    }
    
    function _loadCategory() {
        if (empty($this->_category)) {
            $query = 'select * from #__docario_category where id='.$this->_id;
            $this->_db->setQuery($query);
            $this->_category = $this->_db->loadObject();
            return $this->_category;
        }
    }
    
    function _initCategory() {
        if (empty($this->category)) {
            $category = new stdClass();
            $category->id = 0;
            $category->name = '';
            $category->alias = '';
            $category->description = '';
            $category->published = '';
            $this->_category = $category;
            return (boolean) $this->_category;
        }
        return true;
    }
    
    function getCategory() {
        if ($this->_loadCategory()) {
        } else $this->_initCategory();
        return $this->_category;
    }
    
    function deleteCategory($id) {
        $query = 'delete from #__docario_category';
        $query .= ' where id='.$id;
        
        $this->_db->setQuery($query);
        return $this->_db->query();
    }
    
    function saveCategory() {
        $id = JRequest::getVar('category_id');
        
        $name = JRequest::getVar('category_name');
        $alias = JRequest::getVar('category_alias');
        $description = JRequest::getVar('category_description', '', 'post', 'string', 2);
        $filter = new JFilterInput(array(), array(), 1, 1, 0);
        $description = $filter->clean($description);
        
        $parent = JRequest::getVar('category_parent');
        
        if ($id == 0) {
            $query = 'insert into #__docario_category (name, alias,';
            $query .= ' description, parent_id) values ("'.$name.'",';
            $query .= ' "'.$alias.'", "'.$description.'", '.$parent.')';
        } else {
            $query = 'update #__docario_category set name="'.$name.'",';
            $query .= ' alias="'.$alias.'", description="'.$description.'",';
            $query .= ' parent_id='.$parent.' where id='.$id;
        }
        
        $this->_db->setQuery($query);
        return $this->_db->query();
    }
    
    function updatePublished($id, $status) {
        
    }
    
    function existPerm($category_id, $user_id) {
        $query = 'select count(*) from #__docario_category_user_allowed';
        $query .= ' where userid='.$user_id.' and category_id='.$category_id;
        
        $this->_db->setQuery($query);
        return ($this->_db->loadResult() > 0);
    }
        
    function setPerm($category_id, $user_id, $perm, $perm_mode) {
    /*
    * $perm_mode = 0 Inherited
    * $perm_mode = 1 Allowed
    * $perm_mode = 2 Not allowed
    *
    * $perm = 0 read
    * $perm = 1 write
    */
        switch ($perm) {
            case 0: $perm = 'read'; break;
            case 1: $perm = 'write'; break;
            default: $perm = '';
        }
        
        if (!$this->existPerm($category_id, $user_id)) {
            $query = 'insert into #__docario_category_user_allowed';
            $query .= ' (userid, category_id, `read`, `write`)';
            $query .= ' values ('.$user_id.', '.$category_id.', 0, 0)';
            
            $this->_db->setQuery($query);
            $this->_db->query();
        }
         
        $query = 'update #__docario_category_user_allowed';
        $query .= ' set `'.$perm.'`='.$perm_mode.' where userid='.$user_id;
        $query .= ' and category_id='.$category_id;
        
        $this->_db->setQuery($query);
        $this->_db->query();
        
    }
    
    function setGroupPerm($category_id, $group_id, $perm, $perm_mode) {
    /**
    * $perm_mode = 0 Not allowed
    * $perm_mode = 1 Allowed
    * 
    * $perm = 0 read
    * $perm = 1 write
    */
        switch ($perm) {
            case 0: $perm = 'read'; break;
            case 1: $perm = 'write'; break;
            default: $perm = '';
        }
        
        if (!$this->existGroupPerm($category_id, $group_id)) {
            $query = 'insert into #__docario_category_group_allowed';
            $query .= ' (group_id, category_id, `read`, `write`)';
            $query .= ' values ('.$group_id.', '.$category_id.', 0, 0)';
            
            $this->_db->setQuery($query);
            $this->_db->query();
        }
        
        $query = 'update #__docario_category_group_allowed';
        $query .= ' set `'.$perm.'`='.$perm_mode.' where group_id='.$group_id;
        $query .= ' and category_id='.$category_id;
        
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function existGroupPerm($category_id, $group_id) {
        $query = 'select count(*) from #__docario_category_group_allowed';
        $query .= ' where category_id='.$category_id;
        $query .= ' and group_id='.$group_id;
        
        $this->_db->setQuery($query);
        return $this->_db->loadResult();
    }
    
    function publish($publish, $id) {
        $query = 'update #__docario_category set published='.$publish;
        $query .= ' where id='.$id;
        
        $this->_db->setQuery($query);
        $this->_db->query();
    }
    
    function getCategories() {
        $query = 'select * from #__docario_category where published=1';
        $this->_db->setQuery($query);
        return $this->_db->loadObjectList();
    }
}
